using System.Collections.Generic;
using UnityEngine;

/*
背包
用于拾取物品，选择物品，使用物品。
*/

public class Bag : MonoBehaviour
{
    public Entity entity;
    /// <summary>
    /// 最大物品数量
    /// </summary>
    public int maxItem = 3;
    /// <summary>
    /// 获取背包里最后一个Item
    /// </summary>
    public int fristItemId{
        get{
            for(int i = 0; i<itemList.Count; i++){
                if(itemList[i] != null){
                    return i;
                }
            }
            return -1;
        }
    }
    public int lastItemId{
        get{
            for(int i = itemList.Count-1; i>=0; i--){
                if(itemList[i] != null){
                    return i;
                }
            }
            return -1;
        }
    }
    /// <summary>
    /// 物品列表
    /// </summary>
    [SerializeField]
    private List<Item> itemList = new List<Item>();
    
    /// <summary>
    /// 物品数量
    /// </summary>
    [SerializeField]
    private int _itemCount;
    public int itemCount{
        get{
            int count = 0;
            for(int i = 0; i<itemList.Count; i++){
                if(itemList[i] != null){//不为空物品
                    count++;
                }
            }
            return count;
        }
    }
    
    /// <summary>
    /// 添加Item到背包中，指定位置
    /// </summary>
    /// <param name="item"></param>
    /// <param name="id"></param>
    /// <returns>返回是否添加成功</returns>
    public bool AddItem(Item item, int id){
        if(id >= itemList.Count || id < 0){
            return false;
        }else{
            if(itemList[id] != null)
                return false;
            itemList[id] = item;
            return true;
        }
    }
    /// <summary>
    /// 添加Item到背包里，优先填充下标小的位置
    /// </summary>
    /// <param name="item"></param>
    public bool AddItem(Item item){
        for(int i = 0; i<itemList.Count; i++){
            if(itemList[i] == null){
                return AddItem(item, i);
            }
        }
        return false;
    }

    /// <summary>
    /// 移除一个Item
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    public bool RemoveItem(Item item){
        for(int i = 0; i<itemList.Count; i++){
            if(itemList[i] == item ){
                itemList[i].Reset();
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// 移除指定位置的Item
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public bool RemoveItem(int id){
        if(id >= itemList.Count || id < 0){
            return false;
        }else{
            if(itemList[id] == null)
                return false;
            itemList[id] = null;
            return true;
        }
    }
    
    /// <summary>
    /// 移动物品，返回被替换的物品
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public Item MoveItem(int a, int b){
        if(a >= itemList.Count || a < 0 || b >= itemList.Count || b < 0 || itemList[a].id == 0){
            return null;
        }
        Item temp = itemList[b];
        itemList[b] = itemList[a];
        itemList[a] = null;
        return temp;
    }
    public DropItem Drop(int id){
        if(itemList[id] != null){
            DropItem di = DropItem.CreatDropItem(entity, itemList[id], entity.dropPos);
            RemoveItem(id);
            return di;
        }
        return null;
    }
    public DropItem Drop(bool last = false){
        if(last){
            return Drop(lastItemId);
        }else{
            return Drop(fristItemId);
        }
    }
    
    void OnCollisionEnter2D(Collision2D other){
        //碰撞物品时将其捡起
        if(other.gameObject.tag == "DropItem"){
            DropItem di = other.gameObject.GetComponent<DropItem>();
            _itemCount = itemCount;
            // 检查是否可以拾取
            if(!di.IgnoreCheck(gameObject.tag) && entity != null && _itemCount < maxItem){
                //Debug.Log(AddItem(di.item));
                AddItem(di.item);
                di.item.OnPick(entity);
                di.item.state = ItemState.InContain;
                di.Disappear();
            }
        }
    }
    
    void Awake(){
        //按照容量生成物品列表
        for(int i = 1; i<=maxItem; i++){
            itemList.Add(null);
        }
        if(entity == null){
            Debug.Log("你没有给背包绑定实体！背包来源：" + gameObject);
        }
    }
}
